package io.dy.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.dy.clients.AccountClient;
import io.dy.mapper.OrderMapper;
import io.dy.model.OrderModel;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author dmj
 * @date 2023年10月12日 下午9:43
 */
@Service
@RequiredArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderModel> implements OrderService {

    private final AccountClient accountClient;

    private final OrderMapper orderMapper;

    @Override
    @GlobalTransactional
    public Boolean createOrderAndDeductionBalance(OrderModel orderModel) {

        //1、创建订单
        orderMapper.insert(orderModel);

        //2、扣减账户余额
        Boolean isSuccess = accountClient.deductionBalance(1L, BigDecimal.valueOf(20L));
        return isSuccess;
    }
}
